package com.itxiaowu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itxiaowu.constant.MessageConstant;
import com.itxiaowu.entity.PageResult;
import com.itxiaowu.entity.QueryPageBean;
import com.itxiaowu.entity.Result;
import com.itxiaowu.pojo.CheckGroup;
import com.itxiaowu.service.CheckGroupService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 检查组
 */
@RestController
@RequestMapping("/checkgroup")
public class CheckGroupController {

    @Reference
    private CheckGroupService checkGroupService;


    /**
     * 查询分页信息
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage.do")
//    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean)
    {
        return checkGroupService.findPage(queryPageBean);
    }

    /**
     *  添加检查组
     */
    @RequestMapping("/addcheckgroup.do")
//    @PreAuthorize("hasAuthority('CHECKGROUP_ADD')")
    public Result addCheckGroup(Integer[] checkitemIds,@RequestBody CheckGroup checkGroup){
        try{
            checkGroupService.addCheckGroup(checkitemIds,checkGroup);
            return new Result(true,MessageConstant.ADD_CHECKGROUP_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
    }

    /**
     * 检查组数据回显: 根据id 获得数据
     */
    @RequestMapping("/queryById.do")
    public Result queryById(Integer id){

        try{
            /*获得数据*/
            CheckGroup c = checkGroupService.queryById(id);
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,c);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }

    }

    /**
     *  检查组信息回显， 获得全部的检查组全部信息
     */
    @RequestMapping("/findAll.do")
    public Result findAll(){
        try{
            List<CheckGroup> list = checkGroupService.findAll();
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,list);
        }catch(Exception e){
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }
    /**
     * 根据检查组查询第三张表的关联数据
     */
    @RequestMapping("/findById.do")
    public Result findById(Integer id){

        try{
            List<Integer> ids = checkGroupService.findById(id);
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,ids);
        }catch(Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }

    }
    /**
     * 编辑重新设置检查组和检查项信息
     */
    @RequestMapping("/edit.do")
//    @PreAuthorize("hasAuthority('CHECKGROUP_EDIT')")
    public Result edit(Integer[] checkitemIds,@RequestBody CheckGroup checkGroup){

        try{
            checkGroupService.edit(checkitemIds,checkGroup);
            return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
        }

    }
    /**
     * 删除检查组
     */
    @RequestMapping("/deletecheckgroup.do")
//    @PreAuthorize("hasAuthority('CHECKGROUP_DELETE')")
    public Result delete(Integer id){

        try{
            checkGroupService.delete(id);
            return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        }catch(Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }

    }
}
